package com.woniu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.mapper.AlarmSettingMapper;
import com.woniu.mapper.ChargerMapper;
import com.woniu.mapper.ChargingStationMapper;
import com.woniu.service.AlarmMessageService;
import com.woniu.mapper.AlarmMessageMapper;
import com.woniu.util.PageData;
import com.woniu.util.ResponseData;
import com.woniu.util.easyExcelUtil.EasyExcelUtil;
import com.woniu.vo.AlarmMessageVo;
import com.woniu.vo.MsgExcelVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;


/**
* @author Apricity
* @description 针对表【alarm_message(报警消息表)】的数据库操作Service实现
* @createDate 2025-02-13 20:16:58
*/
@Service
@Slf4j
public class AlarmMessageServiceImpl extends ServiceImpl<AlarmMessageMapper, AlarmMessage>
    implements AlarmMessageService{

    @Autowired
    private AlarmMessageMapper alarmMessageMapper;
    //充电桩
    @Autowired
    private ChargerMapper chargerMapper;
    //报警设置
    @Autowired
    private AlarmSettingMapper alarmSettingMapper;
    //充电站
    @Autowired
    private ChargingStationMapper chargingStationMapper;
    @Override
    public Page<AlarmMessageVo> queryPage(PageData pageData) {
        Page<AlarmMessageVo> page = new Page<>(pageData.getPageNum(), pageData.getPageSize());
        // MyBatis-Plus 分页查询，自动计算 total
        IPage<AlarmMessageVo> alarmMessageVoIPage = alarmMessageMapper.queryMsgInfoPage(page, (String) pageData.getParam());
        return (Page<AlarmMessageVo>) alarmMessageVoIPage;
    }


    @Override
    public AlarmMessageVo queryMsgByid(Long id) {
        AlarmMessageVo alarmMessageVo = alarmMessageMapper.queryMsgById(id);
        Long chargerId = alarmMessageVo.getChargerId();

        Charger charger = chargerMapper.selectById(chargerId);
        BeanUtils.copyProperties(charger, alarmMessageVo);


        return alarmMessageVo;
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        AlarmMessage alarmMessage = new AlarmMessage();
        alarmMessage.setId(id);
        alarmMessage.setStatus(status);

        //根据id改状态
        alarmMessageMapper.updateById(alarmMessage);
    }

    @Override
    public AlarmMessageVo getChargerMsg(Long id) {
        AlarmMessageVo msgVo = alarmMessageMapper.selectByMsgId(id);

        log.info("msgVo----------------------> {}", msgVo);

        return msgVo;
    }

    @Override
    public ResponseData<?> outDepartmentExcelTemplate(HttpServletResponse response) throws IOException {
        String fileName = "报警信息模板";

        // **模拟获取数据库数据**
        List<MsgExcelVo> dataList = getMaintenancePersons();

        // **设置 Excel 响应头**
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="
                + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");

        EasyExcelUtil.writeWithSheetsWeb(response, fileName)
                .writeModel(MsgExcelVo.class, dataList, "msgName1")
                .finish();
        return ResponseData.ok();
    }

    private List<MsgExcelVo> getMaintenancePersons() {
        ArrayList<MsgExcelVo> list = new ArrayList<>();

        List<MsgExcelVo> msgExcelVo = alarmMessageMapper.selectExcelVo();
        for (MsgExcelVo excelVo : msgExcelVo) {
            MsgExcelVo excel = new MsgExcelVo();

            BeanUtils.copyProperties(excelVo, excel);

            list.add(excel);
        }

        return list;
    }
}




